package ca.uwindsor.cs.deepweb.estimation.method.border;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.search.Hits;

public class MatrixNewFilterSetB {

	protected String Field;

	protected Set<String> uniqueid;

	protected HashMap<String, Set<String>> m;

	protected HashMap<String, Integer> inverseDocumentWeight;

	protected HashMap<String, Float> QueryWeight;

	protected Set<String> filterSet;

	public MatrixNewFilterSetB(String FieldName, Set<String> filterSet) {
		uniqueid = new HashSet<String>();
		Field = FieldName;
		m = new HashMap<String, Set<String>>();
		inverseDocumentWeight = new HashMap<String, Integer>();
		QueryWeight = new HashMap<String, Float>();
		this.filterSet = filterSet;
	}

	public void addQuery(String q, Hits hits) {
		int index;
		int length = hits.length();
		String id;
		Set<String> documents = new HashSet<String>(length);
		try {
			for (index = 0; index < length; index++) {
				id = hits.doc(index).get(Field);
				if (filterSet.contains(id)) {
					documents.add(id);
					uniqueid.add(id);
				}
			}
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		m.put(q, documents);
	}

	public void addQuery(String q, Set<String> newIDs) {
		Iterator<String> it = newIDs.iterator();
		String id;
		while(it.hasNext()){
			id = it.next();
			if(!filterSet.contains(id)){
				it.remove();
			}
		}
		m.put(q, newIDs);
	}

	public void calculateInverseDocumentWeight() {
		Set<Entry<String, Set<String>>> row = m.entrySet();
		for (Entry<String, Set<String>> e : row) {
			Set<String> docs = e.getValue();
			for (String docid : docs) {
				if (!inverseDocumentWeight.containsKey(docid)) {
					inverseDocumentWeight.put(docid, 1);
				} else {
					int i = inverseDocumentWeight.get(docid);
					i++;
					inverseDocumentWeight.put(docid, i);
				}
			}
		}
	}

	public void calculateQueryWeight() {
		Set<Entry<String, Set<String>>> row = m.entrySet();
		for (Entry<String, Set<String>> e : row) {
			float weight = 0;
			String query = e.getKey();
			Set<String> docs = e.getValue();
			for (String docid : docs) {
				if (inverseDocumentWeight.containsKey(docid)) {
					weight += 1 / inverseDocumentWeight.get(docid).floatValue();
				}
			}
			QueryWeight.put(query, weight);
		}
	}

	public int getDocumentFrequencybyTerm(String term) {
		Set<String> s = m.get(term);
		if (s != null) {
			return s.size();
		} else {
			return 0;
		}
	}

	/**
	 * @return the documentWeight
	 */
	public HashMap<String, Integer> getInverseDocumentWeight() {
		return inverseDocumentWeight;
	}

	/**
	 * @return the queryWeight
	 */
	public HashMap<String, Float> getQueryWeight() {
		return QueryWeight;
	}

	public double getMeanQueryWeight() {
		double mean = 0;
		double sum = 0;
		Set<Entry<String, Float>> row = QueryWeight.entrySet();
		for (Entry<String, Float> e : row) {
			sum += e.getValue();
		}
		mean = sum / QueryWeight.size();
		return mean;
	}

	public Set<String> getUniqueIDset() {
		return this.uniqueid;
	}

	public HashMap<String, Set<String>> getMatrix() {
		return this.m;
	}

	public static void main(String[] args) {
		// String dictFile=args[0];
		// RandomWordSelector selector= new RandomWordSelector(dictFile,
		// "UTF-8");
		// IndexSearcher searcher;
		// MatrixNewFilterSetB m = new
		// MatrixNewFilterSetB(FilesContentIndexer.FIELD_ID);
		// try {
		// searcher = new IndexSearcher(
		//"d:\\学习资料\\Graduate Study\\Research\\Experiment\\en_wiki_sample_2000")
		// ;
		// QueryParser queryparser;
		// queryparser = new QueryParser(FilesContentIndexer.FIELD_CONTENT,
		// new StandardAnalyzer());
		// Hits hits = null;
		// Query query = null;
		// for (int k = 1; k <= 1; k++) {
		// query = queryparser.parse("test");
		// hits = searcher.search(query);
		// m.addQuery("test", hits);
		// }
		// } catch (CorruptIndexException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// } catch (ParseException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// m.calculateInverseDocumentWeight();
		// m.calculateQueryWeight();
		// System.out.println(m.getMeanQueryWeight());
	}

}
